#include "BuildOrderManager.h"

#include "MacroTask.h"
#include "TaskScheduler.h"
#include "TaskExecutor.h"
#include "BuildUnitManager.h"
#include "..\Util.h"

namespace BroodWar
{
	namespace Addon
	{
		BuildOrderManager::BuildOrderManager(BWSAL::BuildOrderManager* manager)
		{
			instance = manager;
		}
		
		BuildOrderManager::~BuildOrderManager()
		{
			BWSAL::BuildOrderManager::destroy();
		}
		
		BuildOrderManager::!BuildOrderManager()
		{
			BWSAL::BuildOrderManager::destroy();
		}

		void BuildOrderManager::Create(ITaskScheduler^ taskScheduler, ITaskExecutor^ taskExecutor, IBuildUnitManager^ buildUnitManager)
		{
			TaskScheduler^ scheduler = DirectCast<TaskScheduler^>(taskScheduler);
			TaskExecutor^ executor = DirectCast<TaskExecutor^>(taskExecutor);
			BuildUnitManager^ manager = DirectCast<BuildUnitManager^>(buildUnitManager);
			BWSAL::BuildOrderManager::destroy();
			s_instance = gcnew BuildOrderManager(BWSAL::BuildOrderManager::create(scheduler->instance, executor->instance, manager->instance));
		}
		
		BuildOrderManager^ BuildOrderManager::Instance::get()
		{
			return s_instance;
		}

		void BuildOrderManager::OnFrame()
		{
			instance->onFrame();
		}

		void BuildOrderManager::Draw(int x, int y)
		{
			instance->draw(x, y);
		}
		
		MacroTask^ BuildOrderManager::Build(int count, Api::Enum::UnitType t, int priority, TilePosition^ seedLocation)
		{
			return ConvertMacroTask(instance->build(count, ConvertUnitType(t), priority, ConvertTile(seedLocation)));
		}
		
		MacroTask^ BuildOrderManager::Build(int count, Api::Enum::UnitType t, int priority)
		{
			return ConvertMacroTask(instance->build(count, ConvertUnitType(t), priority));
		}
		
		MacroTask^ BuildOrderManager::BuildAdditional(int count, Api::Enum::UnitType t, int priority, TilePosition^ seedLocation)
		{
			return ConvertMacroTask(instance->buildAdditional(count, ConvertUnitType(t), priority, ConvertTile(seedLocation)));
		}
		
		MacroTask^ BuildOrderManager::BuildAdditional(int count, Api::Enum::UnitType t, int priority)
		{
			return ConvertMacroTask(instance->buildAdditional(count, ConvertUnitType(t), priority));
		}
		
		MacroTask^ BuildOrderManager::BuildAdditional(int count, BuildType t, int priority, TilePosition^ seedLocation)
		{
			return ConvertMacroTask(instance->buildAdditional(count, ConvertBuild(t), priority, ConvertTile(seedLocation)));
		}
		
		MacroTask^ BuildOrderManager::BuildAdditional(int count, BuildType t, int priority)
		{
			return ConvertMacroTask(instance->buildAdditional(count, ConvertBuild(t), priority));
		}
		
		MacroTask^ BuildOrderManager::Research(TechType t, int priority)
		{
			return ConvertMacroTask(instance->research(ConvertTech(t), priority));
		}
		
		MacroTask^ BuildOrderManager::Upgrade(int level, UpgradeType t, int priority)
		{
			return ConvertMacroTask(instance->upgrade(level, ConvertUpgrade(t), priority));
		}
		
		void BuildOrderManager::DeleteMacroTask(MacroTask^ mt)
		{
			instance->deleteMacroTask(ConvertMacroTask(mt));
		}
	}
}
